﻿using System;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using System.Xml.Serialization;
using System.IO;
using Emico.Linq.Criterion;
using System.Collections.Generic;
using System.Runtime.Serialization.Formatters.Binary;

namespace Emico.Core.Tests.Criterion
{
    [TestClass]
    public class CriteriaHelperTest
    {
        [TestMethod]
        public void TestStringSerialize()
        {
            string propertyName = "Name";

            LambdaExpression<TestType, string> e = new LambdaExpression<TestType, string>((t, A0) => t.Name == A0, propertyName);
            OrderBy<TestType>[] o = new OrderBy<TestType>[]
            {
                new OrderBy<TestType>(t => t.Name)
            };

            Criteria<TestType> criteria = new Criteria<TestType>(e, o);
            string source = CriteriaHelper.SerializeToString<TestType>(criteria);
            var result = CriteriaHelper.DeserializeFromString<TestType>(source);

            Assert.AreEqual(1, result.Orders.Count);
            Assert.IsTrue(result.Filter is LambdaExpression<TestType>);
        }

        [TestMethod]
        public void TestBinarySerialize()
        {
            string propertyName = "Name";

            LambdaExpression<TestType, string> e = new LambdaExpression<TestType, string>((t, A0) => t.Name == A0, propertyName);
            OrderBy<TestType>[] o = new OrderBy<TestType>[]
            {
                new OrderBy<TestType>(t => t.Name)
            };

            Criteria<TestType> criteria = new Criteria<TestType>(e, o);
            byte[] bytes = CriteriaHelper.SerializeToBinary<TestType>(criteria);
            var result = CriteriaHelper.DeserializeFromBinary<TestType>(bytes);

            Assert.AreEqual(1, result.Orders.Count);
            Assert.IsTrue(result.Filter is LambdaExpression<TestType>);
        }
    }
}
